package com.gitee.mars.studyabroad.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.studyabroad.constant.Constant;
import com.gitee.mars.studyabroad.entity.Country;
import com.gitee.mars.studyabroad.mapper.CountryMapper;
import com.gitee.mars.studyabroad.mapper.UserCollectAbroadSchoolMapper;
import com.gitee.mars.studyabroad.request.AbroadSchoolRequest;
import com.gitee.mars.studyabroad.response.AbroadSchoolResponse;
import com.gitee.mars.studyabroad.response.base.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.gitee.mars.studyabroad.service.IAbroadSchoolService;
import com.gitee.mars.studyabroad.mapper.AbroadSchoolMapper;
import com.gitee.mars.studyabroad.entity.AbroadSchool;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * AbroadSchool 实现层
 *
 * @Author wq
 * @Date 2021-04-21
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class AbroadSchoolServiceImpl extends ServiceImpl<AbroadSchoolMapper, AbroadSchool> implements IAbroadSchoolService {

    @Resource
    private CountryMapper countryMapper;

    @Resource
    private UserCollectAbroadSchoolMapper userCollectAbroadSchoolMapper;

    @Override
    public PageInfo<AbroadSchoolResponse> list(AbroadSchoolRequest request) {
        IPage<AbroadSchool> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<AbroadSchool> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(request.getName())) {
            wrapper.like(AbroadSchool::getName, request.getName());
        }
        if (Objects.nonNull(request.getCountryId())) {
            wrapper.eq(AbroadSchool::getCountryId, request.getCountryId());
        }

        wrapper.orderByDesc(AbroadSchool::getCreateTime);
        IPage<AbroadSchool> abroadSchoolIPage = baseMapper.selectPage(page, wrapper);
        List<AbroadSchoolResponse> list = abroadSchoolIPage.getRecords().stream().map(x -> {
            AbroadSchoolResponse response = new AbroadSchoolResponse();
            BeanUtils.copyProperties(x, response);
            Country country = countryMapper.selectById(x.getCountryId());
            if (Objects.nonNull(country)) {
                response.setCountryName(country.getName());
            }
            Integer integer = userCollectAbroadSchoolMapper.selectIsCollectSchool(request.getUserId(), x.getId());
            response.setIsCollectSchool(integer > Constant.ZERO);
            return response;

        }).collect(Collectors.toList());
        return new PageInfo<>(page.getTotal(), list);
    }
}